/**
 * Created by admin on 16/2/21.
 */
//对link的处理
'use strict';

//屏蔽尾缀数组
let apStr = [
    'jpg',
    'png',
    'ico',
    'gif',
    'tga',
    'css',
    'exe',
    'dmg',
    'apk',
    'rar',
    'js'
];
let apReg = new RegExp('\\.(:?' + apStr.join(')?(:?') + ')?$', 'i');
let formatLink = (link, doc) => {

    //空链接,舍弃
    if (link.length == 0)return false;

    //以某些文件开头的,比如 .css .png .ico 统统舍弃
    if (link.match(apReg !== null))return false;

    //https开头的,老夫没有解密函数啊

    //  以'javascript'开头的,舍弃
    if (link.match(/^javascript:/) !== null)return false;
    //  以'/'开头的,补全Host
    if (link.match(/^\//) !== null) {
        let host = doc.link.match(/(https?:\/\/[^\/]*)/)[1];
        link = host + link;
    }
    //非http://且非https://开头的,补全path
    if (link.match(/^https?/) === null) {
        let path = doc.link.match(/(https?:\/\/[^#?]*\/)/)[1];
        link = path + link;
    }

    return link;
};

module.exports = formatLink;